import { postorder } from '../../utils/api';
const to = require('../../utils/jump')
import '../../utils/lodash_fix'
let _ = require("../../utils/lodash.min");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    paylist: [
      { name: '30天', num: 30 },
      { name: '90天', num: 90 },
      { name: '365天', num: 365 },
    ],
    paynav: 0,
    info: {}
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({ info: wx.getStorageSync('payhouse') })
    console.log(this.data.info)
  },
  selpay(e) {
    let { i, num } = e.currentTarget.dataset
    this.setData({ paynav: i, days: num })
  },
  pay(e) {
    let { num } = e.currentTarget.dataset
    let info = this.data.info
    wx.showModal({
      title: '物业缴费',
      content:
        '当前缴费房屋为:' + info.community_name + info.building_num + '号楼' + info.unit_num + '单元' + info.house_num + '\r\n' +
        '房屋面积为：' + info.area_size + '㎡' + '\r\n' +
        '小区收费标准为：每平方米' + info.property_unit_price / 100 + '元' + '\r\n' +
        '预估物业费为：' + num * info.area_size * info.property_unit_price / 100 + '元',
      success: res => {
        if (res.confirm) {
          postorder({ days: num, house_uuid: this.data.info.uuid }).then(res => {
            console.log(res)
            if (_.isEqual(res.statusCode, 200)) {
              wx.showLoading({
                title: '支付中',
              })
              let payment = res.data.payment
              wx.requestPayment({
                ...payment,
                success: res => {
                  wx.hideLoading()
                  wx.showToast({
                    title: '支付成功,正在跳转...',
                    icon: 'none',
                  })
                  setTimeout(e => {
                    to('/pages/pausu/paysu')
                  }, 1000)
                },
                fail: err => {
                  wx.hideLoading()
                  wx.showToast({
                    title: '支付失败',
                    icon: 'error',
                  })
                }
              })
            }
          })
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    wx.removeStorageSync('payhouse')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})